<?php
/**
 * Created by PhpStorm.
 * User: iiinsomnia
 * Date: 2017/11/25
 * Time: 22:19
 */
namespace App\Service;

use App\Models\TaskDaily;

class TaskDailyService {
    // 添加任务
    public function add($taskID, $input) {
        $model = new TaskDaily();

        $user = UserService::authUser();

        $model->uid = $user->id;
        $model->content = $input['content'];
        $model->img = $input['img'];
        $model->task_id = $taskID;
        $model->created_at = date('Y-m-d H:i:s');
        $model->updated_at = date('Y-m-d H:i:s');

        if (!$model->save()) {
            return false;
        }
        //发送客服消息
        return $model->id;
    }

    public function getList($taskID, $input) {
        $user = UserService::authUser();

        $maxID = isset($input['max_id']) ? intval($input['max_id']) : 0;
        $limit = isset($input['limit']) ? intval($input['limit']) : 10;

        $query = TaskDaily::where('uid', $user->id)->where('task_id', $taskID);

        if ($maxID > 0) {
            $query->where('id', '<', $maxID);
        }

        $models = $query->orderBy('id', 'desc')->limit($limit)->get();

        if (empty($models)) {
            return [];
        }

        $data = $models->toArray();

        foreach ($data as &$v) {
            $v['img'] = env('IMG_DOMAIN', 'http://hackathon.miaobe.com/') . $v['img'];
        }

        return $data;
    }

    public function praise($id) {
        $model = new TaskDaily($id);

        $model->state = 1;

        $result = $model->save();

        return $result;
    }
}